package com.isoftstone.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.isoftstone.pojo.Payment;
import com.isoftstone.service.PaymentService;
import com.isoftstone.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api")
public class PaymentController {

    private final PaymentService paymentService;

    @Autowired
    public PaymentController(PaymentService paymentService) {
        this.paymentService = paymentService;
    }
    //获取所有缴费信息
    @GetMapping("/payment")
    public Result<?> getAllPayments(@RequestParam(defaultValue = "1") Integer currentPage,
                                   @RequestParam(defaultValue = "3") Integer pageSize){
        Page page = paymentService.getAllPayments(currentPage, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
    }
    //根据id获取缴费信息
    @GetMapping("/payment/{id}")
    public ResponseEntity<Payment> getPaymentById(@PathVariable Integer id) {
        return paymentService.getPaymentById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    //添加缴费信息
    @PostMapping("/payment")
    @ResponseStatus(HttpStatus.CREATED)
    public Result<?> insertPayment(@RequestBody Payment payment) {
        return paymentService.insertPayment(payment);
    }
    //修改缴费信息
    @PutMapping("/payment/{id}")
    public ResponseEntity<Result<?>> updatePayment(@PathVariable Integer id, @RequestBody Payment paymentDetails) {
        try {
            Result<?> updatedPayment = paymentService.updatePayment(id, paymentDetails);
            return ResponseEntity.ok(updatedPayment);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    //删除缴费信息
    @DeleteMapping("/payment/{id}")
    public ResponseEntity<Void> deletePayment(@PathVariable Integer id) {
        paymentService.deletePayment(id);
        return ResponseEntity.noContent().build();
    }
    //根据学期进行查询
    @GetMapping("/payment/semester")
    public Result<?> selectPaymentsBySemester(@RequestParam String semester,
                                              @RequestParam(defaultValue = "1") Integer currentPage,
                                              @RequestParam(defaultValue = "3") Integer pageSize){
        Page page = paymentService.selectPaymentsBySemester(semester, currentPage, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
    }
    //根据学年进行查询
    @GetMapping("/payment/academicYear")
    public Result<?> selectPaymentsByAcademicYear(@RequestParam String academicYear,
                                                 @RequestParam(defaultValue = "1") Integer currentPage,
                                                 @RequestParam(defaultValue = "3") Integer pageSize){
        Page page = paymentService.selectPaymentsByAcademicYear(academicYear, currentPage, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
                                                 }
}
